/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mudalov.rec.web;

import com.mudalov.rec.dao.model.user.User;
import com.mudalov.rec.util.webui.ResourcesHelper;
import com.sun.rave.web.ui.appbase.AbstractSessionBean;
import com.sun.webui.jsf.model.UploadedFile;
import java.io.File;
import java.io.Serializable;
import javax.faces.FacesException;

/**
 * @version FileUploadBean.java
 * @version Created on 13.12.2008, 17:07:15
 * @author MU
 */
public class FileUploadBean extends AbstractSessionBean implements Serializable {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }
    // </editor-fold>


    public FileUploadBean() {
    }

    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("FileUploadBean Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    @Override
    public void passivate() {
    }

    @Override
    public void activate() {
    }

    @Override
    public void destroy() {
    }

    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    protected SessionBean1 getSessionBean() {
        return (SessionBean1)getBean("SessionBean1");
    }

    transient private UploadedFile uploadedFile;

    public String getStringContent() {
        return uploadedFile.getAsString();
    }

    public UploadedFile getUploadedFile() {
        return this.uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public String getUserMeterialDir() {
        User user = getSessionBean().getCurrentUser();
        return ResourcesHelper.getUploadDirectory() +
                File.separator + user.getLogin() +
                File.separator + "materials";
    }
}
